package com.cartoon.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.cartoon.R;
import com.cartoon.adapter.CartoonDetailsTypeAdapter;
import com.cartoon.databinding.CarrtonDetailsActivityBinding;
import com.cartoon.databinding.CartoonSearchrecordActivityBinding;
import com.cartoon.dialog.CartoonDirectoryDialog;

import java.util.List;

import base.QActivity;
import base.QLoading;

import constant.AppConstants;
import http.Api;
import io.reactivex.annotations.Nullable;
import model.CarttonKKDirectoryModel;
import model.CarttonLRYDirectoryModel;
import pic.PicLoadManager;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CartoonDetailsActivity  extends QActivity {

    private int kkid;
    private ViewDataBinding CarrtonDetailsActivityBinding;
    private com.cartoon.databinding.CarrtonDetailsActivityBinding binding;
    private CartoonDetailsTypeAdapter detailsTypeAdapter;
    private List<CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean> comics;
    private QLoading dialog;
    private String lryId;
    private int type;
    private List<CarttonLRYDirectoryModel.DataBean.ChapterListBean> chapterList;
    private String lryPic;
    private String kkname;
    private String lryname;

    @Override
    public int getLayoutRes() {
        return R.layout.carrton_details_activity;

    }

    @Override
    public void initView() {

        binding = DataBindingUtil.setContentView(this, getLayoutRes());
        Bundle bundle = getIntent().getExtras();
        kkid = bundle.getInt("kkid",0);
        lryId = bundle.getString("lryId");
        type = bundle.getInt("type", 0);


    }

    @Override
    public void initData() {


        LinearLayoutManager typelayoutManager = new LinearLayoutManager(this);
        typelayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.rvDetailsType.setLayoutManager(typelayoutManager);
        detailsTypeAdapter = new CartoonDetailsTypeAdapter(R.layout.carrton_details_type_item, null);
        binding.rvDetailsType.setAdapter(detailsTypeAdapter);


        binding.tvName.setSelected(true);
    }

    @Override
    public void initListener() {

        binding.ivBlack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.tvDirectory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                chapterList

                if (type == 1) {
                    CartoonDirectoryDialog directoryDialog = new CartoonDirectoryDialog(comics,kkname);
                    directoryDialog.show(getSupportFragmentManager(),"");
                } else if (type == 2) {
                    CartoonDirectoryDialog directoryDialog = new CartoonDirectoryDialog(chapterList,lryPic,lryname);
                    directoryDialog.show(getSupportFragmentManager(),"");

                }


            }
        });
    }

    @Override
    public void networkMessage() {
        dialog = new QLoading();
        dialog.showNow(getSupportFragmentManager(),"");

        // 视频 1是YH  2是LRY  漫画 1是快看 2是LRY
        if (type == 1) {
            getKKCartoonDetailed(kkid);
        } else if (type == 2) {
            getLRYCartoonDetailed(lryId);
        }

    }


    private void getLRYCartoonDetailed(String lryId) {

        Call<CarttonLRYDirectoryModel> call = Api.getInstance().service.getKLRYCartoonDetailed(lryId);
        call.enqueue(new Callback<CarttonLRYDirectoryModel>() {
            @Override
            public void onResponse(Call<CarttonLRYDirectoryModel> call, Response<CarttonLRYDirectoryModel> response) {
                LogUtils.d("网络请求数据" + call.toString(), response.body());

                if (response.body() != null && response.body().getData() != null) {
                    dialog.dismiss();
                    requestWebPhotoBitmap(response.body().getData().getCover());
                    lryPic = response.body().getData().getCover();
                    PicLoadManager.loadPicture(response.body().getData().getCover(),binding.ivPig);
                    CarttonLRYDirectoryModel.DataBean data = response.body().getData();

                    binding.tvName.setText(data.getTitle());
                    lryname = data.getTitle();

//                    detailsTypeAdapter.replaceData(data.getTags());
                    binding.tvBiref.setText(data.getDescs());
                    binding.tvUpdate.setText(data.getUpdateTime());

                    chapterList = data.getChapterList();

//                    comics
//                    lrySearchAnimeAdapter.replaceData(response.body().getData());
                } else {
                    if(response.body()!=null && response.body().getMsg()!=null){
                        ToastUtils.showLong(response.body().getMsg() + "状态码:" + response.code());
                    }
                    dialog.dismiss();

                }

            }

            @Override
            public void onFailure(Call<CarttonLRYDirectoryModel> call, Throwable t) {
                LogUtils.d("请切换模式试下  网络请求失败:" + t.getMessage());
                ToastUtils.showLong("请切换模式  网络请求失败:" + t.getMessage());
            }
        });
    }

    public void getKKCartoonDetailed(int  kkid) {

        Call<CarttonKKDirectoryModel> call = Api.getInstance().service.getKKCartoonDetailed(kkid);
        call.enqueue(new Callback<CarttonKKDirectoryModel>() {
            @Override
            public void onResponse(Call<CarttonKKDirectoryModel> call, Response<CarttonKKDirectoryModel> response) {
                LogUtils.d("网络请求数据" + call.toString(), response.body());

                if (response.body() != null && response.body().getData() != null) {
                    dialog.dismiss();
                    requestWebPhotoBitmap(response.body().getData().getTopic_info().getCover_image_url());
                    PicLoadManager.loadPicture(response.body().getData().getTopic_info().getVertical_image_url(),binding.ivPig);
                    CarttonKKDirectoryModel.DataBean.TopicInfoBean data = response.body().getData().getTopic_info();
                    binding.tvName.setText(data.getTitle());

                    detailsTypeAdapter.replaceData(data.getTags());
                    binding.tvBiref.setText(data.getDescription());
                    binding.tvUpdate.setText(data.getUpdate_status());

                    comics = data.getComics();
                    kkname = data.getTitle();
//                    comics
//                    lrySearchAnimeAdapter.replaceData(response.body().getData());
                } else {
                    if(response.body()!=null && response.body().getMessage()!=null){
                        ToastUtils.showLong(response.body().getMessage() + "状态码:" + response.code());
                    }
                    dialog.dismiss();

                }

            }

            @Override
            public void onFailure(Call<CarttonKKDirectoryModel> call, Throwable t) {
                LogUtils.d("请切换模式试下  网络请求失败:" + t.getMessage());
                ToastUtils.showLong("请切换模式  网络请求失败:" + t.getMessage());
            }
        });
    }





    /**
     * 获取 网络图片 Bitmap
     * @param imgUrl 网络图片url
     */
    private void requestWebPhotoBitmap(String imgUrl) {
        Glide.with(getApplicationContext()).asBitmap().load(imgUrl).into(new CustomTarget<Bitmap>() {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {


//                Bitmap bitmap = ImageUtils.renderScriptBlur(resource, 24);
                Bitmap bitmap = ImageUtils.stackBlur (resource, 24);
                binding.ivBg.setImageBitmap(bitmap);
            }
            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });
    }
}
